package com.example.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.bean.User;
import com.example.common.RedisCode;
import com.example.config.AuthAccess;
import com.example.until.Result;
import com.example.until.UserDto;
import com.example.service.UserService;
import com.example.until.UserEmail;
import com.example.until.UserUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @title: Login
 * @Author wjf
 * @Date: 2022/9/22 10:48
 * @Version 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/sys")
public class LoginAndRegisterController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @AuthAccess
    @PostMapping("/loginAccount")
    public Result loginAccount(HttpServletRequest request, @RequestBody UserDto userDto) {
        String username = userDto.getUsername();
        String password = userDto.getPassword();
        String code = userDto.getCode();
        String s = stringRedisTemplate.opsForValue().get(RedisCode.IMGCODE);
        if (s==null){
            return Result.fail().message("验证码过期");
        }
        System.out.println(code);
        System.out.println(s);
        if (!code.equalsIgnoreCase(s)){
            return Result.fail().message("验证码错误");
        }
        if (StringUtils.isNotBlank(username) || StringUtils.isNotBlank(password))  {
            UserDto user = userService.login(userDto);
            return Result.ok(user);
        }
        return Result.fail().message("参数错误");
    }

    /**
     * 通过邮箱登录
     * @param userDto
     * @return
     */
    @AuthAccess
    @PostMapping("/loginEmail")
    public Result loginEmail(@RequestBody UserDto userDto) {
        String email = userDto.getEmail();
        String code = userDto.getCode();
        if (StringUtils.isBlank(email) || StringUtils.isBlank(code)){
            return Result.fail().message("参数错误");
        }
       UserDto user = userService.loginEmail(email,code);
        return Result.ok(user);
    }

    @AuthAccess
    @PutMapping("/reset")
    public Result reset01(HttpServletRequest request,@RequestBody UserEmail userEmail){
        String email = userEmail.getEmail();
        String code = userEmail.getCode();
        if (StringUtils.isBlank(email) || StringUtils.isBlank(code)){
            return Result.fail().message("参数错误");
        }
        String s = stringRedisTemplate.opsForValue().get(email);
        System.out.println(s);
        String email1 = (String) request.getSession().getAttribute("email");
        System.out.println(email1);
        if (!email.equals(email1)){
            return Result.fail().message("邮箱错误");
        }
        if (s==null){
            return Result.fail().message("验证码过期,请重试");
        }
        if (!code.equalsIgnoreCase(s)){
            return Result.fail().message("验证码错误");
        }

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",email);
        User user = new User();
        user.setPassword("123");
        boolean update = userService.update(user, queryWrapper);
        return Result.ok(update);
    }

    /**
     * 通过邮箱发送验证码
     * @param email
     * @param type
     * @return
     */
    @AuthAccess
    @GetMapping("/email/{email}/{type}")
    public Result getCode(HttpServletRequest request,@PathVariable String email,@PathVariable Integer type){
        if (StringUtils.isBlank(email)){
            return Result.fail().message("参数错误");
        }
        if (type==null){
            return Result.fail().message("参数错误");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",email);
        User one = userService.getOne(queryWrapper);
        if (one!=null){
            userService.sendEmail(email,type);
            request.getSession().setAttribute("email",email);
            return Result.ok();
        }
        return Result.fail().message("未注册邮箱");
    }



    @PostMapping("/register")
    public Result register(@RequestBody UserDto userDto){
        userDto.setRole("学生");
       User user = userService.register(userDto);
       return Result.ok(user);
    }

    @PostMapping("/password")
    public Result select(@RequestBody UserUpdate userUpdate){
       Result result= userService.select(userUpdate);
       return result;
    }

    @PostMapping("/update")
    public Result update(@RequestBody UserUpdate userUpdate){
        Integer update = userService.update(userUpdate);
        if (update!=0){
            return Result.ok();
        }
        return Result.fail().message("参数错误");
    }

}

